
import subscribeComponent from "../component/subscribe_component.vue";
export default {
  components: { subscribeComponent },
  data() {
    return {
      is_has_password:false,
      subscribe_title: "",
      activity_id: "",
      tabbar_active: 0,
      index: 0,
      sex: ["会员", '管理员'],
      type: ['登录密码', '支付密码'],
      radio: 1,
      count: '',
      show_dialog: false,
      show1: false,
      cservice: "",
      service_mobile: "",
      service_QRcode: "",
      active: 0,
      require_pwd: true,
      isMiniApp: window.isMiniprogram ? true : false,
      password: '',
      show_pay: false,
      pay_index: 0,
      role: "member",
      listData: [],
      active_value: 0,//活跃值
      is_admin: false,
      datas:{},
      password_show:false,
      carousel_h5_link: "", banner_h5_link: "", banner_thumb_url: '', carousel_imgs_url: [], sign_multiple: 0, activity_list: []
    }
  },
  activated() {
    this.init()
    this.getData();
    this.getKeFu()
    this.get_buy_data();
    this.getBalancePwdInfo();
  },
  methods: {
    init(){
      this.activity_id = "";
      this.pay_index = 0;
      this.is_has_password = false;
      this.show_dialog = false;
      this.show_pay = false;
      this.index = 0;
      this.password_show = false;
    },
    getKeFu(){
      $http.get("goods.customer-service.index").then(response => {
        if (response.result == 1) {
          this.cservice = response.data.cservice;
          this.service_QRcode = response.data.service_QRcode;
          this.service_mobile = response.data.service_mobile;
          this.showKefu = true;
        } else {
          this.$dialog.alert({ message: response.msg });
        }
      });
    },
   getBalancePwdInfo(){
    $http.get("member.balance-password.is-has-password", {}, "").then(
      (response)=> {
        if (response.result == 1) {
          this.is_has_password = response.data.is_has
        } else {
          this.$dialog.alert({message:response.msg});
        }
      },
      function(response) {
        console.log(response);
      }
    );
   },
   async  password_handle(type){

      if(type == 'forget'){
        let _url = document.location.href;
        if(this.is_has_password){
          _url =  _url.replace(/sign_in_subscribe\/home/,'member/balance_password');
          window.location.href = _url
        }else{
          _url =  _url.replace(/sign_in_subscribe\/home/,'member/set_balance_password');
          window.location.href = _url
        }

      }else {
        this.password_show = false;
      }
    },
    async get_buy_data() {
      let { result, msg, data: { active_value, is_admin, require_pwd,active_value_name } } = await $http.get("plugin.sign-buy.frontend.user-sign.index");
      if (result == 0) return this.$toast(msg);
        this.active_value = active_value;
        this.require_pwd = require_pwd;
        this.is_admin = is_admin;
        this.active_value_name = active_value_name;
    },
    async getData() {
      let { data, result, msg } = await $http.get("plugin.sign-buy.frontend.sign-buy-page.home");
      if (result == 0) {
        return this.$toast(msg)
      }
      let { carousel_h5_link, banner_h5_link, banner_thumb_url, carousel_imgs_url, sign_multiple, activity_list } = data;
      this.carousel_h5_link = carousel_h5_link
      this.banner_h5_link = banner_h5_link
      this.banner_thumb_url = banner_thumb_url
      this.carousel_imgs_url = carousel_imgs_url
      this.sign_multiple = sign_multiple
      this.activity_list = activity_list

    },
     dialog_sign(data) {
      this.password ='';
      this.count = '';
      this.show_dialog = true;
      this.datas = data;
      this.subscribe_title = data.activity_name;
      this.activity_id = data.stage_id;
    },
    skip(type) {
      if (type == 'carousel_h5_link') {
        window.location.href = this.carousel_h5_link;
      } else {
        window.location.href = this.banner_h5_link;
      }
    },
    link(url) {
      if (url == 'sign_in_subscribe_code') {
        this.$router.push(this.fun.getUrl(url, { type: 1 }))
      } else {
        this.$router.push(this.fun.getUrl(url))
      }
    },
    confirm(type) {
      if (type == 'subscribe') {
        if (this.count <= 0) {
          return this.$toast("请输入有效预约数量")
        }
        if(this.index == 0){
          if(this.require_pwd==1){
            this.show_pay = true;
          }else if(this.require_pwd==0){
            this.sigin_handle()
            this.show_dialog = false;
          }
        }else if(this.index == 1){
          this.sigin_handle()
          this.show_dialog = false;
        }
      }
      if (type == 'pay') {
        if (this.password == '') {
          return this.$toast(`请输入${this.pay_index == 0 ? '登录' : '支付'}密码`);
        }
        this.sigin_handle()
      }
    },

    async sigin_handle() {
      let json = {
        stage_id: this.activity_id,
        amount: this.count,
        admin:  this.index
      }
      if (this.index == 0 && this.require_pwd==1) {
        json.pwd_type = this.pay_index == 0 ? "login" : "pay";
        json.pwd = this.password;
      }

      let { result, msg ,data} = await $http.get("plugin.sign-buy.frontend.user-sign.submit", json)

      if (result == 0)  {
        if(this.index == 0 && this.require_pwd==1 && this.pay_index==1){
          if(data.password_error==1){
            this.password_show = true;
          }else{
            this.$toast(msg);
          }
        }else{
          this.$toast(msg);
        }
        return;
      }
      this.show_pay = false;
      this.show_dialog = false;
      this.$toast(msg);
      this.count='';
      this.index = 0;
      this.password = '';
      this.getData();
      this.get_buy_data();
    },
    finish(){
      this.getData()
    },
    choose(i, type) {
      if (type == 'member' || type=='admin') {
        this.index = i;
        i == 1 ? this.role = 'admin' : this.role = 'member';
      } else {
        this.pay_index = i;
      }
    },

    clickHome() {
      this.$router.push(this.fun.getUrl('home'))
    },

  }
}